package cn.wolfcode.luowowo.mgrsite.controller;

import cn.wolfcode.luowowo.article.domain.StrategyContent;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.service.IStrategyCatalogService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.IStrategyTagService;
import cn.wolfcode.luowowo.article.service.IStrategyThemeService;
import cn.wolfcode.luowowo.common.query.StrategyQueryObject;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/strategyDetail")
public class StrategyDetailController {

    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IStrategyCatalogService catalogService;
    @Reference
    private IStrategyThemeService themeService;
    @Reference
    private IStrategyTagService tagService;

    @RequestMapping("list")
    public String list(@ModelAttribute("qo") StrategyQueryObject qo, Model model) {
        PageInfo pageInfo = strategyDetailService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "strategyDetail/list";
    }

    @RequestMapping("input")
    public String input(Long id, Model model) {
        if (id != null) {
            //查询文章
            StrategyDetail detail = strategyDetailService.get(id);
            //查询文章内容
            StrategyContent content = strategyDetailService.getContent(id);
            //内容设置给文章
            detail.setStrategyContent(content);
            model.addAttribute("entity", detail);
            //标签回显
            model.addAttribute("tags", tagService.getByDetailId(id));
        }

        model.addAttribute("catalogs", catalogService.list());
        model.addAttribute("themes", themeService.list());
        return "strategyDetail/input";
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public AjaxResult saveOrUpdate(StrategyDetail detail, String tags) {
        strategyDetailService.saveOrUpdate(detail, tags);
        return new AjaxResult();
    }

    @RequestMapping("changeState")
    @ResponseBody
    public AjaxResult changeState(Long id, Integer state) {
        strategyDetailService.changeState(id, state);
        return new AjaxResult();
    }


}
